package sunyu.tools.encrypt;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 加密解密工具类
 *
 * @author 孙宇
 */
@Component
public class EncryptTools {

    private static final String defaultCharSet = "UTF-8";//默认字符集
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${encrypt.salt}")
    private String salt;//盐
    @Resource(name = "aesTools")
    private AESTools aesTools;

    /**
     * MD5
     *
     * @param text
     * @return
     */
    public String md5(String text) {
        return new Md5Hash(text).toString();
    }

    /**
     * MD5
     *
     * @param text
     * @param salt 盐
     * @return
     */
    public String md5(String text,
                      String salt) {
        return new Md5Hash(text, salt).toString();
    }

    /**
     * MD5
     *
     * @param text
     * @param salt           盐
     * @param hashIterations 散列次数
     * @return
     */
    public String md5(String text,
                      String salt,
                      int hashIterations) {
        return new Md5Hash(text, salt, hashIterations).toString();
    }

    /**
     * Base64编码
     *
     * @param text
     * @return
     */
    public String base64Encode(String text) {
        return Base64.encodeToString(CodecSupport.toBytes(text, defaultCharSet));
    }

    /**
     * Base64解码
     *
     * @param text
     * @return
     */
    public String base64Decode(String text) {
        return Base64.decodeToString(CodecSupport.toBytes(text, defaultCharSet));
    }

    /**
     * Hex编码
     *
     * @param text
     * @return
     */
    public String hexEncode(String text) {
        return Hex.encodeToString(CodecSupport.toBytes(text, defaultCharSet));
    }

    /**
     * Hex解码
     *
     * @param text
     * @return
     */
    public String hexDecode(String text) {
        return new String(Hex.decode(CodecSupport.toBytes(text, defaultCharSet)));
    }

    /**
     * sha256
     *
     * @param text
     * @return
     */
    public String sha256(String text) {
        return new Sha256Hash(text).toString();
    }

    /**
     * sha512
     *
     * @param text
     * @return
     */
    public String sha512(String text) {
        return new Sha512Hash(text).toString();
    }

    /**
     * AES加密
     *
     * @param text
     * @param password
     * @return
     */
    public String aesEncrypt(String text,
                             String password) {
        try {
            return aesTools.encrypt(text, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES解密
     *
     * @param text
     * @param password
     * @return
     */
    public String aesDecrypt(String text,
                             String password) {
        try {
            return aesTools.decrypt(text, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
